#ifndef __PVMVOICEOGGVORBIS_H__
#define __PVMVOICEOGGVORBIS_H__

#include "pvm/pvmapi.h"
#include "lib/pvmvoice.h"
#include "lib/decoders/oggvorbis/pvmovdatasource.h"
#include "lib/decoders/oggvorbis/pvmoggincludes.h"

#include "lib/memory/pvmmemory.h"

class PVMVoiceOggVorbis : public IPVMVoice, public PVMAllocableObject
{
protected:
	PVMVoiceOggVorbis();
public:
	virtual ~PVMVoiceOggVorbis();

	virtual void GetFormat(pvm_uint32 &samplesPerSecond, pvm_uint32 &bitsperSample, pvm_uint32 &chanels);
	virtual pvm_uint32 GetSamplesPerSecond();
	virtual pvm_uint32 GetBitsPerSample();
	virtual pvm_uint32 GetChanelsNum();

	virtual pvm_uint32 FillBuffer(void *buffer, pvm_uint32 numSamples);
	virtual void SetPause(pvm_bool pause);
	virtual pvm_bool GetPause();
	virtual pvm_bool IsAlive();
	virtual void DoDestroy();

	virtual void OnIdle();

	virtual void SetLoopCount(pvm_int32 loopCount);
	virtual pvm_int32 GetLoopCount();
	virtual pvm_bool SetLoopInSamples(pvm_int64 loopFrom, pvm_int64 loopTo);
	virtual pvm_bool GetLoopInSamples(pvm_int64 &loopFrom, pvm_int64 &loopTo);
	virtual pvm_bool GetLoopInMS(pvm_int32 &loopFrom, pvm_int32 &loopTo);

	virtual pvm_bool SetPositionMS(pvm_int32 position);
	virtual pvm_int32 GetPositionMS();

	virtual pvm_bool GetOption(const pvm_char* optionName, void* optionVal, pvm_uint32* optionSize);
	virtual pvm_bool SetOption(const pvm_char* optionName, void* optionVal, pvm_uint32 optionSize);

	static PVMVoiceOggVorbis* Create(PVMOVDataSource* dataSource, int samplesPerSecond, int bitsPerSample, int chanelsNum);
protected:

	bool Init(PVMOVDataSource* source, int bitsPerSample);
	long decode(char *buffer, int lengthInSamples, int sampleSizeInBytes);
	void loadTotalLengthInSamples();
protected:
	PVMOVDataSource*	dataSource;
	vorbis_info*		vInfo;
	pvm_bool isAlive;
	pvm_bool paused;
	pvm_int32 loopCount;
	int bitstream;
	pvm_int64 loopStart;
	pvm_int64 loopEnd;
	pvm_int64 totalLengthInSamples;
	pvm_int64 offsetInSamples;
	pvm_uint32 bitsPerSample;
};

#endif
